package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.cupons.entity.CuponsInfo;
import com.dingpeikeji.quickdeer.core.modules.cupons.service.TCuponsService;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.service.OfficeService;
import com.dingpeikeji.quickdeer.modules.base.service.FrontUserService;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.ShareRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroup;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.FRentproductService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.RentproductOrderService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.ShareProductService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentgroupService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentproductService;
import com.dingpeikeji.quickdeer.utils.BackResult;
import com.dingpeikeji.quickdeer.utils.UUIDUtils;

/**
 * 接送机拼车接口
 *
 */
@Controller
@RequestMapping(value = "/f/mobile")
public class ShareAirRentController extends BaseController {

	@Autowired
	private TRentproductService tRentproductService;
	@Autowired
	private TSitedtlService tSitedtlService;
	@Autowired
	private FRentproductService rentproductService;
	@Autowired
	private TCargotypeService cargotypeService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private TRentgroupService tRentgroupService;
	@Autowired
	private TCuponsService tCuponsService;
	@Autowired
	private FrontUserService frontUserService;
	@Autowired
	private ShareProductService shareProductService;
	@Autowired
	private RentproductOrderService rentproductOrderService;
	@Autowired
	private TOrdersService tOrdersService;

	/**
	 *AA拼车生成产品
	 */
	@RequestMapping(value = "genShareAirProduct")
	@ResponseBody
	public BackResult list(String userid,ShareRentproduct shareRentproduct,Double suitrtnprice,  
			HttpServletRequest request) {
		User user = frontUserService.get(request.getParameter("userid"));
		Map<String, Object> map = new HashMap<String, Object>();
		if (user==null) {
			return BackResult.build("400", "获取用户信息失败");
		}
		//根据提交的车型id,获取车型信息
		TCargotype tCargotype = cargotypeService.get(shareRentproduct.getCargotype());
		if (tCargotype==null) {
			return BackResult.build("400", "获取车型信息失败");
		}
		//判断出发日期和截止日期
		if (shareRentproduct.getFromdate().before( shareRentproduct.getEnddate())) {
			return BackResult.build("400", "截止时间不能小于出发时间");
		}
		//根据提交的车队name和abbr查询车队
		String officename = request.getParameter("officename");
		String abbr = request.getParameter("abbr");
		String officeId = officeService.findIdByNameAndAbbr(officename, abbr);
		if (StringUtils.isEmpty(officeId)) {
			return BackResult.build("400", "获取车队信息失败");
		}
		//校验拼车总人数和车型核载人数
		if (Integer.parseInt(shareRentproduct.getMinamount()) > tCargotype.getSeats()) {
			return BackResult.build("400", "拼车人数不能超过车辆核载人数");
		}
		//获取优惠券信息
		CuponsInfo cuponsInfo = new CuponsInfo();
		if (StringUtils.isNotEmpty(shareRentproduct.getCuponsId())) {
			cuponsInfo = tCuponsService.getCuponsByRecordId(shareRentproduct.getCuponsId());
			shareRentproduct.setUsecupons("1");
			shareRentproduct.setCuponsId(shareRentproduct.getCuponsId());
			suitrtnprice = suitrtnprice - Double.parseDouble(cuponsInfo.getPrice());
		}
		//根据人数计算平均价格
		double avgPrice = Math.round(suitrtnprice / Integer.parseInt(shareRentproduct.getMinamount()));
		if (avgPrice<=0) {
			return BackResult.build("400", "订单金额不合法");
		}
		//处理productname
		String productname = shareRentproduct.getProductname();
		if (productname.contains("(接机)")) {
			productname = productname.replace("(接机)", "拼车");
			shareRentproduct.setJsjtype("0");
		}else if (productname.contains("(送机)")) {
			productname = productname.replace("(送机)", "拼车");
			shareRentproduct.setJsjtype("1");
		}
		//初始化shareProduct
		shareRentproduct.setSid(UUIDUtils.getId().substring(0,10));
		shareRentproduct.setOfficeId(officeId);
		shareRentproduct.setProductname(productname);
		shareRentproduct.setCreateBy(user);
		shareRentproduct.setCreateDate(new Date());
		shareRentproduct.setMaxamount(tCargotype.getSeats()+"");
		shareRentproduct.setDaysnum("1");//接送机默认1天
		shareRentproduct.setPurchasenum("1");//拼车只能为1
		shareRentproduct.setAvgprice(avgPrice);
		shareRentproduct.setCurrentsell(0);//初始化已购买人数
		shareRentproduct.setTotalprice(suitrtnprice);//使用优惠券之后的价格
		shareRentproduct.setStatus("0");
		shareRentproduct.setSharetype(ShareRentproduct.SHARE_TYPE_AIR);
		//默认图片
		shareRentproduct.setLinepic("https://www.xiakeyueche.com/userfiles/airport.jpg");
		//插入数据
		int result = shareProductService.insert(shareRentproduct);
		if (result > 0) {
			map.put("sid", shareRentproduct.getSid());
			map.put("avgprice", shareRentproduct.getAvgprice());
			map.put("sharetype", shareRentproduct.getSharetype());
			map.put("productname", shareRentproduct.getProductname());
			map.put("linepic", shareRentproduct.getLinepic());
			return BackResult.ok(map);
		}else{
			return BackResult.build("400", "生成拼车失败");
		}
	}
	//拼车发布须知
	@RequestMapping(value = "genShareNote")
	@ResponseBody
	public BackResult genShareNote(ShareRentproduct shareRentproduct) {
		return BackResult.ok(ShareRentproduct.SHARE_GEN_NOTE);
	}
}




























